﻿<%@ WebHandler Language="C#" Class="DeleteContainerStepPlanData" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;

/**
 * Description: MES工序排产数据删除
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.9.18
 **/

public class DeleteContainerStepPlanData : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "deleteData":
                        r = deleteData(jObject);//删除数据
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //删除数据
    Result deleteData(JObject jObject)
    {
        string sql = "";
        DataTable willBeDeletedData = JsonConvert.DeserializeObject<DataTable>(jObject["willBeDeletedData"].ToString());
        foreach (DataRow dr in willBeDeletedData.Rows)
        {
            string containername = dr["containername"].ToString();
            string workflowstepname = dr["workflowstepname"].ToString();
            string containerid = "";

            //1.删除工序排产报表中间表数据
            sql = string.Format("DELETE r_containerstepplanboard t WHERE t.containername = '{0}' AND t.workflowstepname = '{1}'", containername, workflowstepname);
            dal.ExecuteSql(sql, "MESDB17");

            sql = @"
SELECT csp.containerid,csp.workflowstepid
FROM containerstepplan csp
INNER JOIN containercurrentinfo cci ON cci.containerid = csp.containerid
WHERE cci.containername = '{0}' AND csp.workflowstepname = '{1}'";
            sql = string.Format(sql, containername, workflowstepname);

            DataTable dt = dal.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                containerid = dt.Rows[0]["containerid"].ToString();
                //2.删除工序排产表源数据
                sql = string.Format("DELETE containerstepplan t WHERE t.containerid = '{0}' AND t.workflowstepname = '{1}'", containerid, workflowstepname);
                dal.ExecuteSql(sql);
            }
        }
        Result r = new Result();
        r.data = new DataTable();
        r.total = Convert.ToInt32(willBeDeletedData.Rows.Count);
        r.meta = new Result.Meta(200, "删除成功");
        return r;
    }
}